import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_base/common/fun.dart';
import 'package:loading_indicator/loading_indicator.dart';

/// Create： Zhang 4/1/21 1:08 PM
/// 描述：封装一些通用的控件
///获取一个带有下拉加载更多的listView

///带下拉加载的listView
refreshListView({
  //创建listView的item
  required ListViewItemBuilder itemBuilder,
  //上拉加载回调方法
  LoadingMore? onLoadingMore,
  //下拉刷新回调方法
  required Refresh refresh,
  Callback? onItemClick,
  //listView的内边距
  EdgeInsetsGeometry? padding,
  //item的总数
  int itemCount = 0,
  //分隔线的高度
  double dividerHeight = 1,
  //分隔线的颜色
  Color dividerColor = Colors.black12,
  //下拉刷新的loading圆环的颜色
  Color refreshColor = Colors.blue,
  //加载更多默认展示的文字
  String loadingDesc = "正在加载...",
  //是否开启上拉加载
  bool isNeedLoadingMore = true,
}) {
  //监听页面滑动
  ScrollController _scrollController = ScrollController();
  //滑动到底部逻辑处理,回调给调用者处理
  _scrollController.addListener(
    () {
      if (_scrollController.position.pixels ==
          _scrollController.position.maxScrollExtent) {
        if (onLoadingMore != null) {
          onLoadingMore();
        }
      }
    },
  );

  return RefreshIndicator(
    color: refreshColor,
    child: ListView.separated(
      physics: itemCount < 10
          ? AlwaysScrollableScrollPhysics()
          : BouncingScrollPhysics(),
      controller: _scrollController,
      padding: padding,
      itemCount: isNeedLoadingMore ? (itemCount + 1) : itemCount,
      itemBuilder: (context, index) {
        return index < itemCount
            ? itemBuilder(context, index)
            : Container(
                width: double.infinity,
                alignment: Alignment.center,
                padding: EdgeInsets.all(15),
                child: Text(
                  loadingDesc,
                  style: TextStyle(
                    fontSize: 14,
                    color: Colors.grey,
                  ),
                ),
              );
      },
      separatorBuilder: (BuildContext context, int index) => Divider(
        height: dividerHeight,
        color: dividerColor,
      ),
    ),
    onRefresh: () => Future(refresh),
  );
}

///loadingView
loadingView({
  double size: 30,
  EdgeInsetsGeometry? margin,
  Color color: Colors.blue,
}) =>
    Stack(
      alignment: Alignment.topCenter,
      children: [
        Container(
          margin: margin,
          width: double.infinity,
          height: size,
          alignment: Alignment.center,
          child: SizedBox(
            width: size,
            height: size,
            child: LoadingIndicator(
              color: color,
              indicatorType: Indicator.lineScale,
            ),
          ),
        ),
      ],
    );

///页面加载状态
enum LoadingState {
  LOADING,
  SUC,
  EMPTY,
  ERR,
}

///通用的页面控件,包含四种状态
commonPage({
  required LoadingState state,
  required Widget sucWidget,
  Widget? emptyWidget,
  Widget? errWidget,
  VoidCallback? onFailPageClick,
}) {
  switch (state) {
    case LoadingState.LOADING:
      return loadingView(
        color: Color(0xff77B16E),
        margin: EdgeInsets.all(20),
      );
    case LoadingState.SUC:
      return sucWidget;
    case LoadingState.EMPTY:
      return InkWell(
        highlightColor: Colors.transparent,
        splashColor: Colors.transparent,
        onTap: onFailPageClick,
        child: emptyWidget ??
            Container(
              alignment: Alignment.center,
              child: Text("无数据"),
            ),
      );

    case LoadingState.ERR:
      return InkWell(
        highlightColor: Colors.transparent,
        splashColor: Colors.transparent,
        onTap: onFailPageClick,
        child: errWidget ??
            Container(
              alignment: Alignment.center,
              child: Text("数据错误"),
            ),
      );
  }
}
